/*
 * @Author: 梓岄 3049738225@qq.com
 * @Date: 2025-11-27 22:58:11
 * @LastEditors: 梓岄 3049738225@qq.com
 * @LastEditTime: 2025-11-27 23:19:31
 * @FilePath: /test/test.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include<iostream>
#include<cstdio>
#include<unistd.h>
#include<string>
#include<string.h>
#include<sys/wait.h>


int main()
{
    int pipeid[2]={0};
    int rt=pipe(pipeid);
    if(rt<0)
    {
        perror("pipe");
        exit(1);
    }
    pid_t id=fork();
    if(id<0)
    {
        perror("fork");
        exit(2);
    }
    else if(id==0)
    {
        sleep(3);
        printf("子进程接收中...\n");
        sleep(2);
        close(pipeid[1]);
        char ch[128];
        ssize_t sz = read(pipeid[0],ch,sizeof(ch)-1);
        ch[sz]=0;
        printf("接收完毕!\n");
        sleep(1);
        std::cout<<"接收内容："<<ch<<std::endl;

        exit(0);
    }
    close(pipeid[0]);
    printf("父进程发送中...\n");
    sleep(2);
    char ch[]="I am father";
    write(pipeid[1],ch,strlen(ch));
    printf("发送成功!\n");
    sleep(1);

    waitpid(id,nullptr,0);

    return 0;
}